package com.ihg.hiex.admin.service.account.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.ihg.hiex.admin.dao.account.AccountDao;
import com.ihg.hiex.admin.dao.account.AdministratorDao;
import com.ihg.hiex.admin.service.DataServiceImpl;
import com.ihg.hiex.admin.service.account.AdministratorService;
import com.ihg.hiex.entity.data.conference.ConferenceRoom;
import com.ihg.hiex.entity.user.Account;
import com.ihg.hiex.entity.user.Administrator;
import com.ihg.hiex.entity.user.Role;

/**
 *
 * @author Ark
 *
 */
@Service("administratorService")
public class AdministratorServiceImpl extends DataServiceImpl<Administrator> implements AdministratorService {

    @Dao
    private AccountDao accountDao;
    
    @Dao
    private AdministratorDao administratorDao;

    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AdministratorService#count(java.lang.String)
     */
    public int count(String role) {
        return administratorDao.count(role);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AdministratorService#list(java.lang.String)
     */
    public List<Administrator> list(String role) {
        return administratorDao.list(role);
    }

    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AdministratorService#list(java.lang.String, com.demo2do.core.entity.page.Page)
     */
    public List<Administrator> list(String role, Page page) {
        List<Administrator> administrators = administratorDao.list(role, page.getBeginIndex(), page.getEveryPage());
        page.calculate();
        return administrators;
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AdministratorService#loadByEmail(java.lang.String, java.lang.String)
     */
    public Administrator loadByEmail(String email, String role) {
        return administratorDao.loadByEmail(email, role);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AdministratorService#create(com.ihg.hiex.entity.user.Administrator)
     */
    public void create(Administrator administrator, ConferenceRoom conferenceRoom) {
        
        String email = administrator.getEmail().toLowerCase();

        // 1. load account by email
        Account account = accountDao.loadByEmail(email);
        if (account == null) {              // 2.1 if account no exit

            // 2.1.1 save a new account
            account = new Account(administrator);
            genericDaoSupport.save(account.initialize());

            // 2.1.2 save a new administrator
            administrator.initialize(account, conferenceRoom);
            genericDaoSupport.save(administrator);

        } else {                            // 2.2. if account exit
            
            // 2.2.1 judge account whether existed
            if (account.hasAuthority("ADMIN")) {
                throw BusinessException.newInstance("500", "创建失败，该用户已存在Admin角色");
            }
            
            // 2.2.2 save admin & update account authority
            genericDaoSupport.save(administrator.initialize(account, conferenceRoom));
            genericDaoSupport.update(account.addAuthority("ADMIN"));

        }
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AdministratorService#createCoach(com.ihg.hiex.entity.user.Administrator)
     */
    public void createCoach(Administrator administrator) {
        String email = administrator.getEmail().toLowerCase();

        // 1. load account by email
        Account account = accountDao.loadByEmail(email);
        if (account == null) {              // 2.1 if account no exit

            // 2.1.1 save a new account
            account = new Account(administrator.getEmail(), Role.COACH.toString(), administrator.getTelephone());
            genericDaoSupport.save(account.initialize());

            // 2.1.2 save a new administrator
            administrator.initialize(account);
            genericDaoSupport.save(administrator);

        } else {                            // 2.2. if account exit
            
            // 2.2.1 judge account whether existed
            if (account.hasAuthority("COACH")) {
                throw BusinessException.newInstance("500", "创建失败，该用户已存在Coach角色");
            }
            
            // 2.2.2 save admin & update account authority
            genericDaoSupport.save(administrator.initialize(account));
            genericDaoSupport.update(account.addAuthority("COACH"));

        }
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AdministratorService#edit(com.ihg.hiex.entity.user.Administrator)
     */
    public void edit(Administrator administrator) {

        // 1. update account
        Account account = administrator.getAccount();
        account.edit(administrator.getTelephone());
        genericDaoSupport.update(account);

        // 2. update administrator
        genericDaoSupport.update(administrator.edit());
        
    }

    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.account.AdministratorService#disable(com.ihg.hiex.entity.user.Administrator)
     */
    public void disable(Administrator administrator) {

        // 1. get account by administrator
        Account account = administrator.getAccount();

        // 2. update account 
        if (account.hasAuthority("ASSESSOR")) {
            genericDaoSupport.update(account.removeAuthority("ADMIN"));
        } else { 
            genericDaoSupport.update(account.disable());
        }

        // 3. disable administrator
        genericDaoSupport.update(administrator.disable());

    }

}
